package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.GetDistributionAgencyConfigReq;
import com.shop2cn.iapi.sqdistributor.parameter.request.pojo.DistributionAgencyLevelCommissionConfigVo;
import com.shop2cn.iapi.sqdistributor.parameter.response.GetDistributionAgencyConfigResponse;
import com.shop2cn.iapi.sqdistributor.service.GetDistributionAgencyConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.sqdistributor.DistributionAgencyCommisionRate;
import com.ymttest.database.model.sqdistributor.DistributionAgencyConfig;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencyCommisionRateWrapper;
import com.ymttest.database.sqlwapper.sqdistributor.DistributionAgencyConfigWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetDistributionAgencyConfig {
	
	//java接口
	private static GetDistributionAgencyConfigReq getDistributionAgencyConfigReq;
	private static GetDistributionAgencyConfigCall getDistributionAgencyConfigCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商户的分销员机构配置");
	}

	@Before
	public void caseUp() {
		getDistributionAgencyConfigReq = new GetDistributionAgencyConfigReq();
		getDistributionAgencyConfigCall = new GetDistributionAgencyConfigCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getDistributionAgencyConfigCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		DistributionAgencyConfigWrapper distributionConfigWrapper = new DistributionAgencyConfigWrapper();
		
		DistributionAgencyConfig actualResult = distributionConfigWrapper
				.selectByMerchantIdAndAgencyType(
						getDistributionAgencyConfigReq.getMerchantId(),
						getDistributionAgencyConfigReq.getAgencyType());

		if (null == actualResult) {
			
			Logger.comment("校验distribution_agency_config表");
			
			GetDistributionAgencyConfigResponse expectResult = getDistributionAgencyConfigCall
					.getDistributionAgencyConfigResponse();
			
			Logger.verifyEquals(expectResult.getMerchantId(),
					expectResult.getMerchantId(), "验证Merchant_id");
			Logger.verifyEquals(expectResult.getDistributionSettlementDelayDays(),
					0, "验证Distribution_settlement_delay_days");
			
			List<DistributionAgencyLevelCommissionConfigVo> expectResultList = expectResult
					.getDistributionAgencyLevelCommissionConfigVos();
			
			Logger.comment("校验distribution_agency_commision_rate表");
			
			DistributionAgencyLevelCommissionConfigVo expectDistributorLevelCommissionConfigVoResult = expectResultList
					.get(0);

			Logger.verifyEquals(expectDistributorLevelCommissionConfigVoResult
					.getDistributionConfigId(), 0, "验证Distribution_config_id");
			Logger.verifyEquals(expectDistributorLevelCommissionConfigVoResult
					.getDistributorLevelIndex(), 0, "验证Distributor_level_index");
			Logger.verifyEquals(expectDistributorLevelCommissionConfigVoResult
					.getDistributorLevelLabel(), "普通机构", "验证Distributor_level_label");
			DataUtil.verifyDecimalEquals(
					expectDistributorLevelCommissionConfigVoResult
							.getLayer1Rate(), null, "验证Layer1_rate");
		} else {
			
			Logger.comment("校验distribution_agency_config表");
			
			GetDistributionAgencyConfigResponse expectResult = getDistributionAgencyConfigCall
					.getDistributionAgencyConfigResponse();
			
			Logger.verifyEquals(expectResult.getMerchantId(),
					actualResult.getMerchant_id(), "验证Merchant_id");
			Logger.verifyEquals(expectResult.getDistributionSettlementDelayDays(),
					actualResult.getDistribution_settlement_delay_days(), "验证Distribution_settlement_delay_days");
		
			Logger.verifyEquals(expectResult.getSettlementPeriodicityType(),
					actualResult.getSettlement_periodicity_type(), "验证SettlementPeriodicityType");
			// 如果商户配置了微信直连，则Settlement_way一直为1
			Logger.verifyEquals(expectResult.getSettlementWay(),
					actualResult.getSettlement_way(), "验证Settlement_way");
			Logger.verifyEquals(expectResult.getAgencyType(),
					actualResult.getAgency_type(), "验证AgencyType");
			
			List<DistributionAgencyLevelCommissionConfigVo> expectResultList = expectResult
					.getDistributionAgencyLevelCommissionConfigVos();
			
			DistributionAgencyCommisionRateWrapper commisionRateWrapper = new DistributionAgencyCommisionRateWrapper();
			DistributionAgencyCommisionRate actualCommisionRateResult = commisionRateWrapper.selectByConfigId(actualResult.getId());

			Logger.comment("校验distribution_agency_commision_rate表");
			
			DistributionAgencyLevelCommissionConfigVo expectDistributorLevelCommissionConfigVoResult = expectResultList
					.get(0);

			Logger.verifyEquals(expectDistributorLevelCommissionConfigVoResult
					.getDistributionConfigId(), actualCommisionRateResult
					.getDistribution_config_id().intValue(),
					"验证Distribution_config_id");
			Logger.verifyEquals(expectDistributorLevelCommissionConfigVoResult
					.getDistributorLevelIndex(), actualCommisionRateResult
					.getDistributor_level_index(), "验证Distributor_level_index");
			Logger.verifyEquals(expectDistributorLevelCommissionConfigVoResult
					.getDistributorLevelLabel(), actualCommisionRateResult
					.getDistributor_level_label(), "验证Distributor_level_label");
			DataUtil.verifyDecimalEquals(
					expectDistributorLevelCommissionConfigVoResult
							.getLayer1Rate(), actualCommisionRateResult
							.getLayer1_rate(), "验证Layer1_rate");
			Logger.verifyEquals(expectDistributorLevelCommissionConfigVoResult
					.getBindingEffectiveThreshold(), actualCommisionRateResult
					.getBinding_effective_threshold(),
					"验证BindingEffectiveThreshold");
			Logger.verifyEquals(expectDistributorLevelCommissionConfigVoResult
					.getUserDistributorBindRuleType(),
					actualCommisionRateResult
							.getUser_distributor_bind_rule_type(),
					"验证UserDistributorBindRuleType");
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDistributionAgencyConfig_001() {
		Logger.start(true, "商户的分销员机构配置，获取成功");
		
		try {
			
			getDistributionAgencyConfigReq.setMerchantId(500861041L);
			getDistributionAgencyConfigReq.setAgencyType(1);
			
			getDistributionAgencyConfigCall.setData(getDistributionAgencyConfigReq);
			getDistributionAgencyConfigCall.callService();
			
			Logger.verifyEquals(true, getDistributionAgencyConfigCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDistributionAgencyConfig_002() {
		Logger.start(true, "获取未设置分销员配置，结果为空");
		
		try {
			
			getDistributionAgencyConfigReq.setMerchantId(502131251L);
			getDistributionAgencyConfigReq.setAgencyType(1);
			
			getDistributionAgencyConfigCall.setData(getDistributionAgencyConfigReq);
			getDistributionAgencyConfigCall.callService();
			
			Logger.verifyEquals(true, getDistributionAgencyConfigCall.getSuccess(), "验证success");
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetDistributionAgencyConfig_003() {
		Logger.start(true, "获取CPS配置，获取成功");
		
		try {
			
			getDistributionAgencyConfigReq.setMerchantId(500861041L);
			getDistributionAgencyConfigReq.setAgencyType(2);
			
			getDistributionAgencyConfigCall.setData(getDistributionAgencyConfigReq);
			getDistributionAgencyConfigCall.callService();
			
			Logger.verifyEquals(true, getDistributionAgencyConfigCall.getSuccess(), "验证success");
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
